/*
 * 	Flan.Zeng 2011-2016	http://git.oschina.net/signup?inviter=flan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.flan.stock.activity;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.flan.stock.R;
import com.flan.stock.StockApplication;
import com.flan.stock.bean.News;
import com.flan.stock.net.ObjectRequest;
import com.flan.stock.view.CustomDialog;
import com.flan.stock.view.FontDialog;
import com.flan.stock.view.SimpleCommonTitleView;
import com.google.gson.reflect.TypeToken;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Base64;
import android.util.TypedValue;
//import android.text.format.DateFormat;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 显示新闻详情的窗口
 * @author zemg
 * @since  2016年4月6日
 */
public class NewsDetailActivity extends Activity {
	
	//通用窗口title
	private SimpleCommonTitleView simpleTitle;
	private TextView name_tv,time_tv,from_tv,fenxiang_tv;
	private Button review_but;
	private WebView webView;
	private int fontSize = 1;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_news_detail);
		
		//初始化titlebar
		simpleTitle = (SimpleCommonTitleView) this.findViewById(R.id.simple_title);
		simpleTitle.setOnLeftClickListener(new MyOnClickListener());
		simpleTitle.setOnRightClickListener(new MyOnClickListener());
		
		//初始化分享按钮
		fenxiang_tv = (TextView) this.findViewById(R.id.fenxiang);
		fenxiang_tv.setOnClickListener(new MyOnClickListener());
		
		name_tv = (TextView) this.findViewById(R.id.news_name);
		time_tv = (TextView) this.findViewById(R.id.news_time);
		from_tv = (TextView) this.findViewById(R.id.news_from);
		review_but = (Button) this.findViewById(R.id.news_review);
		webView = (WebView) this.findViewById(R.id.news_content);
		
		initData();
	}
	
	/**
	 * 加载数据
	 */
	public void initData(){
		//TODO初始化新闻数据
		
		String url = this.getResources().getString(R.string.url_news_detail);
		
		Listener<List<News>> successListener = new Listener<List<News>>() {

			@Override
			public void onResponse(List<News> resultList) {
				//System.out.println("============== "+resultList.get(0).getTitle());
				if(resultList != null && resultList.size() > 0){
					News news = resultList.get(0);
					
					//String html = Base64.encodeToString(news.getContent().getBytes(), Base64.CRLF);
					String html = new String(Base64.decode(news.getContent(), Base64.CRLF));
					webView.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);  
			        webView.getSettings().setJavaScriptEnabled(true);  
			        webView.setWebChromeClient(new WebChromeClient());
				}
				
			}
		};
		
		ErrorListener errorListener = new ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError arg0) {
				System.out.println("============== 失败");
			}
		};
		
		Request<List<News>> newsRequest = new ObjectRequest<>(Method.GET, new TypeToken<List<News>>(){}.getType(), url, successListener,errorListener);
		StockApplication.requestQueue.add(newsRequest);
		
		Intent intent = this.getIntent();
		News news = (News) intent.getSerializableExtra("news");
		name_tv.setText(news.getTitle());
		DateFormat df = new SimpleDateFormat("MM-dd hh:mm",Locale.CHINA);
		time_tv.setText(df.format(news.getCreateTime()));
		from_tv.setText("来源："+news.getFrom());
	}
	
	/**
	 * 内部类 处理按钮点击事件
	 * @author zemg
	 * @since  2016年4月13日
	 */
	private class MyOnClickListener implements View.OnClickListener{
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.title_right_but:
				fontChange();
				break;
			case R.id.title_left_but:
				NewsDetailActivity.this.finish();
				break;
			case R.id.fenxiang:
				fenxiang();
				break;
			}
			
		}
		
		//处理字体切换按钮点击事件
		private void fontChange(){
			FontDialog.Builder builder = new FontDialog.Builder(NewsDetailActivity.this);
			builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int a) {
					
					switch (fontSize) {
						case 0:
							name_tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,24);
							time_tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
							from_tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
							review_but.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
							webView.getSettings().setTextZoom (120);
							break;
						case 1:
							name_tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,20);
							time_tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,12);
							from_tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,12);
							review_but.setTextSize(TypedValue.COMPLEX_UNIT_SP,12);
							webView.getSettings().setTextZoom (100);
							break;
						case 2:
							name_tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,16);
							time_tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,10);
							from_tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,10);
							review_but.setTextSize(TypedValue.COMPLEX_UNIT_SP,10);
							webView.getSettings().setTextZoom (80);
							break;
					}
					dialog.dismiss();
				}
			});
			builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int arg1) {
					dialog.dismiss();
				}
			});
			builder.setSingleChoiceItems(1, fontSize, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int item) {
					fontSize = item;
					//Toast.makeText(NewsDetailActivity.this, "单选按钮："+item, Toast.LENGTH_SHORT).show();
				}
			});
			builder.setTitle("正文字体");
			FontDialog dialog = builder.create();
			dialog.show();
		}
		
		//处理分享点击事件
		private void fenxiang(){
			CustomDialog.Builder builder = new CustomDialog.Builder(NewsDetailActivity.this);
			builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int arg1) {
					dialog.dismiss();
				}
			});
			CustomDialog dialog = builder.create();
			
			//获得当前窗体
			Window window = dialog.getWindow();
			window .setGravity(Gravity.BOTTOM);
			
			WindowManager wm = getWindowManager();
			Display display = wm.getDefaultDisplay();
			android.view.WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();   
			lp.width = display.getWidth();
			lp.height =LayoutParams.WRAP_CONTENT;
			dialog.getWindow().setAttributes(lp);
			
			dialog.show();
		}
	}
	
}


















